<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Media;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class MediaController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('媒体');
            $content->description('管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('媒体');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加');
            $content->description('媒体');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Media::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->column('avatar','媒体')->image('',25,25);

            $grid->column('name','媒体名称');

            $grid->column('info','媒体简介')->limit(50);


            $grid->created_at('添加时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Media::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->text('name', '媒体名称')->setWidth(2);
            $form->image('avatar', '媒体头像')->setWidth(2);
            $form->textarea('info', '媒体简介');
            $form->select('type', '媒体类型')->options(Media::mediaType())->setWidth(2);
            $form->url('href','媒体主页')->default("#");
            $form->image('qr_code','媒体二维码')->setWidth(2);

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    public function getMedia(Request $request)
    {
        $q = $request->get('q');

        return Media::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
    }
}
